package 剑指offer;

public class p4二维数组中的查找 {
    public boolean findNumberIn2DArray1(int[][] matrix, int target) {
        //暴力法，不考虑二维数组排好序
        for (int i = 0; i <matrix.length; i++) {
            for (int j = 0; j <matrix[0].length; j++) {
                if (matrix[i][j]==target) {
                    return true;
                }
            }
        }
        return false;
    }


    public boolean findNumberIn2DArray(int[][] matrix, int target) {
        int row=matrix.length-1;
        int column=0;

        while (row>=0 && column<matrix[0].length) {
            //出二维数组边界，则循环结束返回false
            if (matrix[row][column] > target) {
                //减一行
                row--;
            }else if (matrix[row][column] < target) {
                //减一列
                column++;
            }else {
                //matrix[row][column] == target
                return true;
            }
        }
        return false;
    }


}
